package cn.com.startai.smartadh5.main.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.startai.smartadh5.R;
import cn.com.startai.smartadh5.extension.utils.musicplayer.CoverLoader;
import cn.com.startai.smartadh5.processlogic.entity.bean.ShowFileTypeBean;

/**
 * Created by chufeng on 2018/4/9.
 * 展示文件的适配器
 */

public class ShowFileListAdapter extends RecyclerView.Adapter<ShowFileListAdapter.ViewHolder> {
    private String fileType;
    private String mfileName;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    private long Mtime;
    private String showTime;
    private String mSize;
    private Context mContext;
    private LayoutInflater inflater;
    private List<ShowFileTypeBean.Content> list;
    private String mfielPath;

    public ShowFileListAdapter(Context context, List<ShowFileTypeBean.Content> list) {
        this.mContext = context;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_list, null, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ShowFileTypeBean.Content item= list.get(position);
        mfielPath=item.getFilePath();
        fileType = item.getFileType();
        mfileName = item.getFileName();
        Mtime = item.getTime();
        showTime = dateFormat.format(new Date(Mtime));
        mSize = ConvertUtils.byte2FitMemorySize(item.getFileSize());

        if(list.size()==0){

        }
        Bitmap bitmap;
        try {
            bitmap = CoverLoader.getInstance().createAlbumArt(mfielPath);
        } catch (Exception e) {
            e.printStackTrace();
            bitmap=null;
        }
        //判断数据不为空
        if(mfileName!=null){
            holder.fileName.setText(mfileName);
        }else {
            holder.fileName.setText("未知名称");
        }
        if (showTime!=null){
            holder.fileDate.setText(showTime);
        }else {
            holder.fileDate.setText("未知时间");
        }if(mSize!=null){
            holder.fileSize.setText(mSize);
        }else {
            holder.fileSize.setText("未知大小");
        }

            if (bitmap != null) {
                //加载封面页面
                holder.iconShow.setImageBitmap(bitmap);
            } else {
                if (fileType.equals("musicplayer")) {
                    holder.iconShow.setImageResource(R.drawable.mmusicplayer);
                } else if (fileType.equals("mediaplayer")) {
                    holder.iconShow.setImageResource(R.drawable.vplayer);
                } else if (fileType.equals("imageplayer")) {
                    holder.iconShow.setImageResource(R.drawable.file_img);
                } else if (fileType.equals("other")) {
                    holder.iconShow.setImageResource(R.drawable.ddocuments);
                }
            }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.icon_show)
        ImageView iconShow;
        @BindView(R.id.file_name)
        TextView fileName;
        @BindView(R.id.file_size)
        TextView fileSize;
        @BindView(R.id.file_date)
        TextView fileDate;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
